import 'dart:math';

import 'package:flutter/material.dart';

import 'kq_chart_pie_model.dart';

class KqChartPie extends StatefulWidget {
  ///外围总宽度
  final double? height;

  ///外围总高度
  final double? width;

  ///背景色
  final Color? backGroundColor;

  ///样式模型
  final ChartPieModel chart;

  ///数据
  final List<ChartPieValue>? data;

  ///中心文本文案
  final String centerText;

  ///中心文本样式
  final TextStyle? textStyle;

  ///中心文本行数
  final int? maxLine;
  const KqChartPie(
      {super.key,
      this.height,
      this.width,
      this.backGroundColor,
      this.data,
      this.centerText = '',
      this.textStyle,
      this.maxLine = 1,
      required this.chart});

  @override
  State<KqChartPie> createState() => _KqChartPieState();
}

class _KqChartPieState extends State<KqChartPie> {
  @override
  Widget build(BuildContext context) {
    widget.chart
        .calculateEvenProportion(widget.data ?? widget.chart.chartModel);
    KqPieChartPainter painter = KqPieChartPainter(
        data: widget.chart.chartModel,
        outRadius: widget.chart.outRadius,
        innerRadius: widget.chart.innerRadius);
    return Container(
        width: widget.width,
        height: widget.height,
        color: widget.backGroundColor,
        child: Stack(
          alignment: Alignment.center,
          children: [
            CustomPaint(
              size: Size(widget.chart.sizeWitdh, widget.chart.sizeHeight),
              foregroundPainter:
                  widget.backGroundColor != null ? painter : null,
              painter: widget.backGroundColor == null ? painter : null,
              child: widget.backGroundColor != null
                  ? Container(
                      alignment: Alignment.center,
                      width: widget.chart.sizeWitdh,
                      height: widget.chart.sizeHeight,
                    )
                  : null,
            ),
            Container(
              // color: widget.backGroundColor,
              width: widget.chart.innerRadius * 1.2,
              height: widget.chart.innerRadius * 1.2,
              alignment: Alignment.center,
              child: Text(
                widget.centerText,
                maxLines: widget.maxLine,
                style: widget.textStyle,
              ),
            )
          ],
        ));
  }
}

class KqPieChartPainter extends CustomPainter {
  ///圆心位置
  late Offset circleCenter;

  ///外弧半径
  late double outRadius;

  ///内弧半径
  final double innerRadius;

  /// 数据
  final List<ChartPieValue> data;

  KqPieChartPainter({
    required this.data,
    required this.outRadius,
    required this.innerRadius,
  });

  @override
  void paint(Canvas canvas, Size size) {
    Rect drawArea = Rect.fromLTWH(0, 0, size.width, size.height);
    Offset center = drawArea.center;
    circleCenter = center;
    for (var item in data) {
      // 画扇形
      Paint paint = Paint()
        ..color = item.color
        ..strokeCap = StrokeCap.butt
        ..isAntiAlias = true
        ..strokeWidth = outRadius - innerRadius
        ..style = PaintingStyle.stroke;
      Rect rect = Rect.fromCircle(
          center: center, radius: innerRadius + (outRadius - innerRadius) / 2);
      double round = 2 * pi;
      canvas.drawArc(
          rect, item.startAngle * round, item.proportion * round, false, paint);
    }
    //画内圈
    // Paint whitePaint = Paint()
    //   ..color = Colors.white
    //   ..strokeCap = StrokeCap.round
    //   ..isAntiAlias = true
    //   ..style = PaintingStyle.fill;
    // canvas.drawCircle(center, innerRadius, whitePaint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return this != oldDelegate;
  }
}
